﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using WalkingTec.Mvvm.Core;
using System.Text.Json.Serialization;
using nERP.Model;
using nERP.Model.Bas;
using nERP.Model.So;
using nERP.Model.Wms;
using nERP.Model.Fin;

namespace nERP.Model.So
{
    /// <summary>
    /// 销售订单
    /// </summary>
	[Table("Sale")]
    [SoftKey(nameof(Sale.SaleCode))]
    [Display(Name = "_Model.Sale")]
    public class Sale : BasePoco, IPersistPoco, ITenant, IWorkflow
    {
        [Display(Name = "_Model._Sale._SaleCode")]
        [StringLength(32, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("销售单号")]
        [Required(ErrorMessage = "Validate.{0}required")]
        [CanNotEdit]
        public string SaleCode { get; set; }
        [Display(Name = "_Model._Sale._Customer")]
        [Comment("客户")]
        [NotMapped]
        public Customer Customer { get; set; }
        [Display(Name = "_Model._Sale._Customer")]
        [StringLength(32, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Required(ErrorMessage = "Validate.{0}required")]
        [Comment("客户")]
        public string CustomerId { get; set; }
        [Display(Name = "_Model._Sale._SettlementAccount")]
        [Comment("结算账户")]
        [NotMapped]
        public SettlementAccount SettlementAccount { get; set; }
        [Display(Name = "_Model._Sale._SettlementAccount")]
        [StringLength(32, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("结算账户")]
        public string SettlementAccountId { get; set; }
        [Display(Name = "_Model._Sale._InvoicesDate")]
        [Comment("单据日期")]
        public DateTime? InvoicesDate { get; set; }
        [Display(Name = "_Model._Sale._MakeUser")]
        [Comment("制单人")]
        [NotMapped]
        public FrameworkUser MakeUser { get; set; }
        [Display(Name = "_Model._Sale._MakeUser")]
        [StringLength(32, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("制单人")]
        public string MakeUserId { get; set; }
        [Display(Name = "_Model._Sale._DeliveryDate")]
        [Comment("交货日期")]
        public DateTime? DeliveryDate { get; set; }
        [Display(Name = "_Model._Sale._TotalPrice")]
        [Comment("总金额")]
        public decimal? TotalPrice { get; set; }
        [Display(Name = "_Model._Sale._TotalPrice1")]
        [Comment("已结金额")]
        public decimal? TotalPrice1 { get; set; }
        [Display(Name = "_Model._Sale._TotalPrice2")]
        [Comment("未结金额")]
        public decimal? TotalPrice2 { get; set; }
        [Display(Name = "_Model._Sale._Status")]
        [Comment("单据状态")]
        public StatusTypeEnum? Status { get; set; }
        [Display(Name = "_Model._Sale._InvoicesFile")]
        [Comment("单据附件")]
        public List<SaleInvoicesFile> InvoicesFile { get; set; }
        [Display(Name = "_Model._Sale._Remark")]
        [StringLength(256, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("备注")]
        public string Remark { get; set; }
        [Display(Name = "_Model._SaleDetail._Sale")]
        [InverseProperty("Sale")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.So.SaleDetail.SaleId))]
        public List<SaleDetail> SaleDetail_Sale { get; set; }
        [Display(Name = "_Model._SaleReturn._Sale")]
        [InverseProperty("Sale")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.So.SaleReturn.SaleId))]
        public List<SaleReturn> SaleReturn_Sale { get; set; }
        [Display(Name = "_Model._SaleReturnDetail._Sale")]
        [InverseProperty("Sale")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.So.SaleReturnDetail.SaleId))]
        public List<SaleReturnDetail> SaleReturnDetail_Sale { get; set; }
        [Display(Name = "_Model._SaleOutWarehouse._Sale")]
        [InverseProperty("Sale")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.Wms.SaleOutWarehouse.SaleId))]
        public List<SaleOutWarehouse> SaleOutWarehouse_Sale { get; set; }
        [Display(Name = "_Model._SaleOutWarehouseDetail._Sale")]
        [InverseProperty("Sale")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.Wms.SaleOutWarehouseDetail.SaleId))]
        public List<SaleOutWarehouseDetail> SaleOutWarehouseDetail_Sale { get; set; }
        [Display(Name = "_Model._SaleReturnInWarehouse._Sale")]
        [InverseProperty("Sale")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.Wms.SaleReturnInWarehouse.SaleId))]
        public List<SaleReturnInWarehouse> SaleReturnInWarehouse_Sale { get; set; }
        [Display(Name = "_Model._ReceivableDetail._Sale")]
        [InverseProperty("Sale")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.Fin.ReceivableDetail.SaleId))]
        public List<ReceivableDetail> ReceivableDetail_Sale { get; set; }
        [Display(Name = "_Model._Sale._IsValid")]
        [Comment("是否有效")]
        [Required(ErrorMessage = "Validate.{0}required")]
        public bool IsValid { get; set; } = true;
        [StringLength(50, ErrorMessage = "Validate.{0}stringmax{1}")]
        public string TenantCode { get; set; }

    }
    public class SaleInvoicesFile : TopBasePoco, ISubFile
    {
        public Guid SaleId { get; set; }
        public Sale Sale { get; set; }
        public Guid FileId { get; set; }
        public FileAttachment File { get; set; }
        public int Order { get; set; }
    }

}
